package com.lightjet.macross.base.upms.controller;
import com.lightjet.macross.base.feign.api.controller.PrincipalController;
import com.lightjet.macross.base.upms.domain.SysOrganization;
import com.lightjet.macross.base.upms.service.SysOrganizationService;
import com.lightjet.macross.common.dto.ResponseDTO;
import com.lightjet.macross.common.util.RespUtil;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.*;
import lombok.extern.java.Log;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/org")
@Scope("request")
@Log
@Api(tags="通用系统权限管理-组织相关接口集")
public class OrgController extends PrincipalController { //implements OrgRemoteClient

    @Resource
    private SysOrganizationService sysOrganizationService;


    @RequestMapping(value = "/all", method = {RequestMethod.GET})
    @ApiOperation(value = "所有组织数据接口", notes = "用于查询所有组织集合")
    //@Override
    public ResponseDTO<List<SysOrganization>> GetAllDepts() {
        return RespUtil.CreateOK(sysOrganizationService.FindAll());
    }

    @RequestMapping(value = "/query", method = {RequestMethod.GET})
    @ApiOperation(value = "组织数据查询接口", notes = "根据组织关键字获取组织集合")
    //@Override
    public ResponseDTO<List<SysOrganization>> GetDeptsByKeywords(String keywords) {
        return RespUtil.CreateOK(sysOrganizationService.FindByKeywords(keywords));
    }

    @RequestMapping(value = "", method = {RequestMethod.GET})
    //@Override
    @ApiOperation(value = "组织单体查询接口", notes = "根据组织ID、名称的优先级顺序获取相应组织信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "组织ID",paramType = "query"),
            @ApiImplicitParam(name="code",value = "组织编码",paramType = "query")
    })
    public ResponseDTO<SysOrganization> GetDept(String id, String code) {
        if(!StringUtil.isNullOrEmpty(id))
        {
            return RespUtil.CreateOK(sysOrganizationService.FindDistinctById(id));
        }
        else if(!StringUtil.isNullOrEmpty(code))
        {
            return RespUtil.CreateOK(sysOrganizationService.FindDistinctByOrgancode(code));
        }
        return RespUtil.CreateError("查询参数为空");
    }

    @PostMapping(value ="",consumes = "application/json")
    @ApiOperation(value = "组织新增接口", notes = "根据角色实体对象Json新增")
    public ResponseDTO<SysOrganization> CreateOrg(
            @ApiParam(name = "sysOrganization",value = "组织实体类Json",type="body")@RequestBody SysOrganization sysOrganization){
        if(sysOrganization.getParent()!=null && sysOrganization.getParent().getId() == null)
        {
            sysOrganization.setParent(null);
        }
        return RespUtil.CreateOK(sysOrganizationService.CreateAndFlush(sysOrganization));
    }

    @PutMapping(value ="",consumes = "application/json")
    @ApiOperation(value = "组织更新接口", notes = "根据组织实体对象Json更新")
    public ResponseDTO<SysOrganization> UpdateRole(
            @ApiParam(name = "roleInfo",value = "组织实体类Json",type="body")@RequestBody SysOrganization sysOrganization){
        SysOrganization original = sysOrganizationService.FindDistinctById(sysOrganization.getId());
        if(original == null)
        {
            return RespUtil.CreateError("更新角色【"+sysOrganization.getId()+"】失败");
        }
        else
        {
            original.setSort(sysOrganization.getSort());
            original.setShortname(sysOrganization.getShortname());
            original.setDisplayname(sysOrganization.getDisplayname());
            original.setOrgancode(sysOrganization.getOrgancode());
            original.setMemo(sysOrganization.getMemo());
            original.setIsenable(sysOrganization.getIsenable());
            return RespUtil.CreateOK(sysOrganizationService.UpdateAndFlush(original));
        }
    }

    @DeleteMapping(value = "")
    @ApiOperation(value = "删除组织接口", notes = "根据组织ID删除")
    public ResponseDTO<String> DeleteRole( @ApiParam(name = "id",value = "组织ID",type="query") String id){
        try {
            sysOrganizationService.DeleteOne(new SysOrganization(id));
            return RespUtil.CreateOK();
        }
        catch (Exception e)
        {
            return RespUtil.CreateError("删除失败："+e.getMessage());
        }
    }


    @RequestMapping(value = "/root", method = {RequestMethod.GET})
    @ApiOperation(value = "组织根节点数据查询接口", notes = "获取组织结构树根节点的组织")
    //@Override
    public ResponseDTO<List<SysOrganization>> GetRootOrgs() {
        return RespUtil.CreateOK(sysOrganizationService.FindRootOrgs());
    }
}
